package cn.edu.ctbu.gudie.service;

import cn.edu.ctbu.gudie.domain.*;
import cn.edu.ctbu.gudie.repository.*;
import cn.edu.ctbu.gudie.service.Interface.PatientServiceIn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PatientService implements PatientServiceIn {
    @Autowired
    private PatientRepository patientRepository;
    @Autowired
    private TagsRepository tagsRepository;
    @Autowired
    private DepartmentsRepository departmentsRepository;
    @Autowired
    private DepartmentTagRepository departmentTagRepository;
    @Autowired
    private HospitalsRepository hospitalsRepository;
    @Autowired
    private DepartmentHospitalRepository departmentHospitalRepository;
    @Autowired
    private AdviceRepository adviceRepository;
    /**
     * 获取所有患者信息
     * @param pageable 分页
     * @return
     */
    @Override
    public Page<Patient> findAll(Pageable pageable) {

        return patientRepository.findAll(pageable);
    }
//    /**
//     * 获取所有患者信息
//     * @param pageable 分页
//     * @return
//     */
//    @Override
//    public Page<Advice> getAllAdvices(Pageable pageable) {
//
//        return adviceRepository.getAllAdvices(pageable);
//    }

    /**
     * 通过姓名来查询对应的患者列表
     *
     * @param patientName 指定姓名的患者列表
     * @return
     */
    @Override
    public List<Patient> findByPatientName(String patientName) {
        return patientRepository.findByPatientName(patientName);
    }
    /**
     * 找出所有符号指定姓名和密码的患者信息
     * @param patientName   姓名
     * @param patientPassword   密码
     * @return 患者信息
     */
    @Override
    public List<Patient> findByPatientNameAndPatientPassword(String patientName, String patientPassword) {
        return patientRepository.findByPatientNameAndPatientPassword(patientName, patientPassword);
    }
    public Map<String, Object> recommendDepartmentsAndHospitals(List<Integer> tagIds) {
        Set<Integer> departmentIds = new HashSet<>();

        // 找到匹配的科室
        for (Integer tagId : tagIds) {
            List<DepartmentTag> departmentTags = departmentTagRepository.findByTagId(tagId);
            for (DepartmentTag departmentTag : departmentTags) {
                departmentIds.add(departmentTag.getDepartmentId());
            }
        }

        List<Departments> departments = departmentsRepository.findAllById(departmentIds);

        // 根据科室推荐医院
        List<Hospitals> hospitals = new ArrayList<>();
        for (Integer departmentId : departmentIds) {
            List<DepartmentHospital> departmentHospitals = departmentHospitalRepository.findByDepartmentId(departmentId);
            for (DepartmentHospital departmentHospital : departmentHospitals) {
                Optional<Hospitals> hospital = hospitalsRepository.findById(departmentHospital.getHospitalId());
                hospital.ifPresent(hospitals::add);
            }
        }

        // 按照评分排序医院
        hospitals.sort(Comparator.comparingDouble(Hospitals::getScore).reversed());

        Map<String, Object> recommendations = new HashMap<>();
        recommendations.put("departments", departments);
        recommendations.put("hospitals", hospitals);
        return recommendations;
    }

    /**
     * 保存患者信息
     * @param patient
     * @return
     */
    @Override
    public Patient save(Patient patient) {
        return patientRepository.save(patient);
    }
    /**
     * 保存患者信息
     * @param advice
     * @return
     */
    @Override
    public Advice saveAdvice(Advice advice) {
        return adviceRepository.save(advice);
    }
    @Override
    public void deleteAdvice(Integer id) {
        adviceRepository.deleteById(id);
    }
}
